// 学生管理页面
<template>
  <div class="all">
    <!-- 仅仅显示第一行表头 -->
    <!-- 冒号，即为单向绑定 -->
    <!-- pagination.records直接拿取得整个表单当前的数据 -->
    <el-table :data="pagination.records" border>
      <el-table-column fixed="left" prop="studentName" label="姓名" width="180"></el-table-column>
      <el-table-column prop="institute" label="学院" width="200"></el-table-column>
      <el-table-column prop="major" label="专业" width="200"></el-table-column>
      <el-table-column prop="grade" label="年级" width="200"></el-table-column>
      <el-table-column prop="clazz" label="班级" width="100"></el-table-column>
      <el-table-column prop="sex" label="性别" width="120"></el-table-column>
      <!-- <el-table-column prop="tel" label="联系方式" width="120"></el-table-column> -->
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="checkGrade(scope.row.studentId)" type="primary" size="small">编辑</el-button>
          <el-button @click="deleteById(scope.row.studentId)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页表单 -->
    <el-pagination   
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[6, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page">
    </el-pagination>
    <!-- 编辑对话框-->
    <el-dialog
      title="编辑试卷信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <section class="update">
        <!-- v-model实现双向绑定 -->
        <!-- form即为数据库中的数据 -->
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="form.studentName"></el-input>
          </el-form-item>
          <el-form-item label="学院">
            <el-input v-model="form.institute"></el-input>
          </el-form-item>
          <el-form-item label="专业">
            <el-input v-model="form.major"></el-input>
          </el-form-item>
          <el-form-item label="年级">
            <el-input v-model="form.grade"></el-input>
          </el-form-item>
          <el-form-item label="班级">
            <el-input v-model="form.clazz"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="form.sex"></el-input>
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input v-model="form.tel"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pagination: {
        //分页后的考试信息
        current: 1, //当前页
        total: null, //记录条数
        size: 6, //每页条数
      },
      dialogVisible: false, //对话框
      form: {}, //保存点击以后当前试卷的信息
      
    };
  },
  created() {
    this.getStudentInfo();
    // console.log(App)
    // console.log("hello")
  },
  methods: {
    getStudentInfo() {
      //分页查询所有试卷信息，默认为put请求
        // this.$axios(`/api/student/20154001`).then(res => {
     // 实际拿取了很多数据 
     //   `/api/students/${this.pagination.current}/${this.pagination.size}`
      this.$axios({
            url:'/api/students',
            method: 'get',
            params:{
                    'page':1,
                    'size':6
            }
        }).then(res => {
        this.pagination = res.data.data;
        // console.log(this.pagination)
        console.log(this.pagination)
      }).catch(error => {});
      
    },
    //  每次调整都重新获取数据库信息
    //改变当前记录条数
    handleSizeChange(val) {
      this.pagination.size = val;
      this.getStudentInfo();
    },  // 调整每页最大页数
    //改变当前页码，重新发送请求
    handleCurrentChange(val) {
      this.pagination.current = val;
      this.getStudentInfo();
    },   // 调整页数
    // 绑定了编辑按钮
    checkGrade(studentId) { //修改学生信息
      this.dialogVisible = true
    //   发出get请求，获得当前学生的数据
      this.$axios(`/api/student/${studentId}`).then(res => {
        this.form = res.data.data   // 给form赋值成object对象格式，实际为数据库内的数据
      })
    },
    // submit(){
    //     this.$axios({
    //         url:'/api/hello',
    //         method: 'get',
    //         params:{
    //                 'username':"hello",
    //                 'password':"this.password"
    //             }
    //     })
    // },


    submit() { //提交更改
        // console.log("hello")
      this.dialogVisible = false   // 点击按钮后则隐藏对话框
      this.$axios({
        url: '/api/student',    // url 定位到/student处
        method: 'put',
        data: {   // 给data赋值，为this.form
          ...this.form
        }
      }).then(res => {   // 异步调用，箭头函数
        //  //console.log(res)
        //  console.log(this.form)
        if(res.data.code ==200) {
          this.$message({
            message: '更新成功',
            type: 'success'
          })
        }
         this.getStudentInfo()  // 该行用于数据库修改后即时刷新数据
      })
    },
    handleClose(done) { //关闭提醒
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        }).catch(_ => {});
    },
  }
};
</script>
<style lang="scss" scoped>
.all {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
  .el-table tr {
    background-color: #dd5862 !important;
  }
}
.el-table .warning-row {
  background: #000 !important;
}

.el-table .success-row {
  background: #dd5862;
}
</style>
